package com.dhj.streamTest;

import com.dhj.streamTest.entity.Author;
import com.dhj.streamTest.entity.Book;

import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Stream;

import static com.dhj.streamTest.entity.AuthorList.getAuthors;

public class Demo9 {

    public static void main(String[] args) {
        List<Author> authors = getAuthors();

        authors.stream().distinct()
                .map(author -> author.getBooks()).forEach(new Consumer<List<Book>>() {
            @Override
            public void accept(List<Book> books) {
                for (Book book : books){
                    System.out.println(book.getName());
                }
            }
        });

        System.out.println("--------------------------------");

        authors.stream().distinct().flatMap(new Function<Author, Stream<Book>>() {
            @Override
            public Stream<Book> apply(Author author) {
                return author.getBooks().stream();
            }
        }).forEach(new Consumer<Book>() {
            @Override
            public void accept(Book o) {
                System.out.println(o.getName());
            }
        });

        System.out.println("--------------------------------");

        authors.stream().distinct()
                .flatMap(author -> author.getBooks().stream())
                .forEach(book -> System.out.println(book.getName()));
    }
}
